package com.yunxi.service.dubboService.impl.sto;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.BeanTools;
import com.yunxi.model.order.PmsPartnerStorage;
import com.yunxi.model.order.PmsVehicleStock;
import com.yunxi.model.sto.StoTransportPlan;
import com.yunxi.model.sto.VehicleOffline;
import com.yunxi.service.localService.CodeCreateService;
import com.yunxi.service.localService.VehicleStockService;
import com.yunxi.service.localService.sto.StoTransportPlanService;
import com.yunxi.service.localService.sto.VehicleOfflineService;
import com.yunxi.sto.pojo.StoStockOutPlanVO;
import com.yunxi.sto.pojo.StoTransportPlanPickVO;
import com.yunxi.sto.pojo.StoTransportPlanQueryVO;
import com.yunxi.sto.pojo.StoTransportPlanResVO;
import com.yunxi.sto.pojo.StoTransportPlanVO;
import com.yunxi.sto.service.StoStockOutPlanDubboService;
import com.yunxi.sto.service.StoTransportPlanDubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Date;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 运输计划单 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-23
 */
public class StoTransportPlanDubboServiceImpl implements StoTransportPlanDubboService {

    @Autowired
    @Qualifier(value = "stoTransportPlanServiceImpl")
    StoTransportPlanService stoTransportPlanService;

    @Autowired
    @Qualifier(value = "codeCreateServiceImpl")
    CodeCreateService codeCreateService;

    @Autowired
    @Qualifier(value = "vehicleStockServiceImpl")
    VehicleStockService vehicleStockService;

    @Autowired
    @Qualifier(value = "stoStockOutPlanDubboServiceImpl")
    StoStockOutPlanDubboService stoStockOutPlanDubboService;


    @Override
    public StoTransportPlanVO getById(String id) {
        StoTransportPlan stoTransportPlan = stoTransportPlanService.getById(id);
        StoTransportPlanVO stoTransportPlanVO = new StoTransportPlanVO();
        BeanTools.copyProperties(stoTransportPlanVO,stoTransportPlan);
        return stoTransportPlanVO;
    }

    /**
     * 运输计划维护
     * @param stoTransportPlanVO VO
     */
    @Override
    public void saveOrUpdate(StoTransportPlanVO stoTransportPlanVO) {
        StoTransportPlan stoTransportPlan = new StoTransportPlan();
//        BeanUtils.copyProperties(stoTransportPlan,stoTransportPlanVO);
        BeanUtils.copyProperties(stoTransportPlanVO,stoTransportPlan);
        if(stoTransportPlan.getTransportPlanId() == null){
            String tpp = codeCreateService.createCode("TPP", 4);
            stoTransportPlan.setTransportPlanNo(tpp);
            stoTransportPlan.setBillState("1");//初始单据状态:汇集
        }
        stoTransportPlanService.saveOrUpdate(stoTransportPlan);
    }

    @Override
    public void removeById(List<String> list) throws Exception {
        stoTransportPlanService.cancel(list);
    }

    @Override
    public void route(String transportPlanId, String routeCode) throws Exception {
        stoTransportPlanService.route(transportPlanId,routeCode);
    }

    @Override
    public List carrierList(String routeCode) {
        return stoTransportPlanService.carrierList(routeCode);
    }

    @Override
    public void carrier(String transportPlanId, String carrierCode)throws Exception {
        stoTransportPlanService.carrier(transportPlanId,carrierCode);
    }

    @Override
    public PageInfo<StoTransportPlanResVO> findPageByVO(StoTransportPlanQueryVO stoTransportPlanQueryVO, Integer pageNum, Integer pageSize) {
        PageInfo<StoTransportPlanResVO> pageInfo = stoTransportPlanService.pageByVo(stoTransportPlanQueryVO, pageNum, pageSize);
        return pageInfo;
    }

    /**
     * 根据VIN码查询运输计划是否以下发到TMS
     * @param vin
     * @return
     */
    @Override
    public Boolean checkTransportPlanIsSend(String vin)throws Exception {
        return stoTransportPlanService.checkAndChangeTransportPlan(vin,"1");
    }

    /**
     * 根据VIN码取消新建的运输计划
     * @param vin
     */
    @Override
    public Boolean cancelTransportPlanByVin(String vin) throws Exception{
        return stoTransportPlanService.checkAndChangeTransportPlan(vin,"3");
    }

    /**
     * 提车
     * @param stoTransportPlanPickVO
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void pickVehicle(StoTransportPlanPickVO stoTransportPlanPickVO) throws Exception {
        StoTransportPlan stoTransportPlan = stoTransportPlanService
            .getById(stoTransportPlanPickVO.getTransportPlanId());
        if (stoTransportPlan == null) {
            throw new Exception("操作失败,无法根据主键:找到对应记录!");
        }
        Integer isPicked = stoTransportPlan.getIsPicked();
        if (isPicked != null && isPicked.equals(1)) {
            throw new Exception("操作失败,已提车完成不可再次提车!");
        }
        stoTransportPlan.setIsPicked(1);
        stoTransportPlan.setPickDate(new Date());
        stoTransportPlan.setPickerName(stoTransportPlanPickVO.getPickerName());
        stoTransportPlan.setPickerPhone(stoTransportPlanPickVO.getPickerPhone());
        stoTransportPlanService.saveOrUpdate(stoTransportPlan);

        ////0924 生成出库计划
        StoStockOutPlanVO stoStockOutPlanVO = new StoStockOutPlanVO();
        stoStockOutPlanVO.setVin(stoTransportPlan.getVin());
        // 销售出库
        stoStockOutPlanVO.setStockOutType("1");
        stoStockOutPlanVO.setPlanStockOutDate(new Date());
        stoStockOutPlanDubboService.saveOrUpdate(stoStockOutPlanVO);
    }

}
